<?php
/**
 * User: Drery
 * Date: 2015/11/9
 * Time: 14:25
 */

namespace api\controllers;
use api\behaviors\ApiRecordor;
use api\components\ExpireAccess;
use Yii;
use yii\filters\AccessControl;
use yii\rest\Controller;
use api\behaviors\Auth;
use yii\filters\ContentNegotiator;
use yii\web\Response;

class InitController extends Controller
{
    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator'] = [
            'class' => ContentNegotiator::className(),
            'formats' => [
                'application/json' => Response::FORMAT_JSON,
            ],
        ];
        $behaviors['apiRecordor'] = [
            'class' => ApiRecordor::className(),
        ];
        $behaviors['authenticator'] = [
            'class' => Auth::className(),
            'except' => [
                'user/signup',
                'user/login',
                'user/face-login',
                'user/exist',
                'appversion/index'
            ]
        ];
        $behaviors['expireAuth'] = [
            'class' => AccessControl::className(),
            'except' => [
                'signup',
                'login',
                'face-login',
                'exist'
            ],
            'rules' => [
                [
                    'class' => ExpireAccess::className(),
                    'allow' => true
                ]
            ]
        ];
        return $behaviors;
    }

    public function buildOutput($data,$ret=0,$msg='')
    {
        return ['ret'=>$ret,'data'=>$data,'message'=>$msg];
    }

    public function buildErrorOutput($ret,$msg='')
    {
        $msg = $msg ? $msg : '';
        return $this->buildOutput([],$ret,$msg);
    }
}